package cn.yikaojiuguo.student.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.Category;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.my.InfoChoosePopupWindow;
import de.greenrobot.event.EventBus;

/**
 * Created by cfan_yang on 2016/1/12.
 */
public class DialogChooseAdapter extends SimpleBaseAdapter<Category> {

    private int mType = InfoChoosePopupWindow.CHOOSE_GRADE;

    public DialogChooseAdapter(Context context, List<Category> list) {
        super(context, list);
    }

    public DialogChooseAdapter(Context context, List<Category> list, int type) {
        this(context, list);
        mType = type;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ChooseDialogViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_dialog_list_choose, null);
            holder = new ChooseDialogViewHolder();
            holder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
            holder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            convertView.setTag(holder);
        } else {
            holder = (ChooseDialogViewHolder) convertView.getTag();
        }
        final Category category = getItem(position);
        int visibility = View.INVISIBLE;
        switch (mType) {
            case InfoChoosePopupWindow.CHOOSE_GRADE:
                visibility = category.ename.equals(YiKaoApplication.getGradeName()) ? View
                        .VISIBLE : View.INVISIBLE;
                break;
            case InfoChoosePopupWindow.CHOOSE_MAJOR:
                visibility = category.ename.equals(YiKaoApplication.getMajorName()) ? View
                        .VISIBLE : View.INVISIBLE;
                break;
            case InfoChoosePopupWindow.CHOOSE_PROVINCE:
                visibility = category.ename.equals(YiKaoApplication.getProvinceName()) ? View
                        .VISIBLE : View.INVISIBLE;
                break;
        }
        holder.iv_choose.setVisibility(visibility);
        holder.tv_text.setText(category.ename);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = null;
                String requestUrl = null;
                switch (mType) {
                    case InfoChoosePopupWindow.CHOOSE_GRADE:
                        msg = "年级";
                        requestUrl = UrlConstants.CHANGE_GRADE + "&mid=" + YiKaoApplication
                                .getUserId() + "&grade=" + category.evalue;
                        break;
                    case InfoChoosePopupWindow.CHOOSE_MAJOR:
                        msg = "专业方向";
                        requestUrl = UrlConstants.CHANGE_GRADE + "&mid=" + YiKaoApplication
                                .getUserId() + "&category=" + category.evalue;
                        break;
                    case InfoChoosePopupWindow.CHOOSE_PROVINCE:
                        msg = "生源地";
                        requestUrl = UrlConstants.CHANGE_GRADE + "&mid=" + YiKaoApplication
                                .getUserId() + "&province=" + category.evalue;
                        break;
                }
                requestNet(msg, requestUrl,category.ename,category.evalue + "");
                EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType.DISMISS_POPUP_WINDOW));
            }
        });
        return convertView;
    }

    private void requestNet(final String msg, String requestUrl, final String name, final String id) {
        EventBus.getDefault().post(new MessageEvent("正在修改" + msg + "...", MessageEvent
                .MessageType.SHOW_PROGRESS_DIALOG));
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if(response!=null && response.optBoolean("success")) {
                    EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType
                            .DISMISS_PROGRESS_DIALOG));
                    switch (mType) {
                        case InfoChoosePopupWindow.CHOOSE_GRADE:
                            YiKaoApplication.putGradeId(id);
                            YiKaoApplication.putGradeName(name);
                            YiKaoApplication.changeUserInfoCache(true);
                            break;
                        case InfoChoosePopupWindow.CHOOSE_MAJOR:
                            YiKaoApplication.putMajorId(id);
                            YiKaoApplication.putMajorName(name);
                            break;
                        case InfoChoosePopupWindow.CHOOSE_PROVINCE:
                            YiKaoApplication.putProvinceId(id);
                            YiKaoApplication.putProvinceName(name);
                            break;
                    }
                    EventBus.getDefault().post(new MessageEvent(mType+"",MessageEvent.MessageType.REFRESH_USER_INFO));
                    ToastUtil.showToast(mContext,msg + "修改成功");
                }
            }
        }, null, mContext.getClass().getSimpleName());
    }

}
